/* 
 * File:   main.cpp
 * Author: grm
 *
 * Created on 1 avril 2011, 10:57
 */


#include "decoder.hpp"
#include "encoder.hpp"
#include "wrapper.hpp"
#include <omp.h>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    if (argc != 4) {
        printf("USAGE : %s input_file output_dir working_dir\n", argv[0]);
        return (1);
    }

    char* input_file = argv[1];
    char* output_dir = argv[2];
    char* work_dir = argv[3];

    Decoder* decoder = new Decoder(input_file);

    int i = 0;
    Encoder* encoder = new Encoder(work_dir);
    while (decoder->stillHasFrame()) {
//#pragma omp parallel
        {
            AVFrame* frame = NULL;
            int img_num;
            uint8_t* image_buffer = (uint8_t*) malloc(decoder->getFrameSize());
//#pragma omp critical
            {
                frame = decoder->getNextVideoFrame(image_buffer);
                img_num = i++;
            }
            if (frame != NULL) {
                char filename[50];
                sprintf(filename, "frame-%09d", img_num);

                encoder->encode(frame, filename);
                av_free(frame);
		free(image_buffer);
            }	            
        }
    }

    printf("Encoding finished ...\n");
    fflush(stdout);

    Wrapper* wrapper = new Wrapper(work_dir, output_dir);
    wrapper->wrap();

    delete decoder;
    delete wrapper;
    delete encoder;

    return 0;
}

